<html>

<head>
  <title>离线瓦片地图</title>
  <link rel="stylesheet" type="text/css" href="./ol.css">
  <script type="text/javascript" src="./ol.js" charset="utf-8"></script>
</head>

<body>
  <div id="map" style="width:100%;"></div>

  <script type="text/javascript">
    var projection = ol.proj.get("EPSG:3857");
    var resolutions = [];
    for (var i = 0; i < 19; i++) {
      resolutions[i] = Math.pow(2, 18 - i);
    }
    var tilegrid = new ol.tilegrid.TileGrid({
      origin: [0, 0],
      resolutions: resolutions
    });

    var baidu_source = new ol.source.TileImage({
      projection: projection,
      tileGrid: tilegrid,
      tileUrlFunction: function (tileCoord, pixelRatio, proj) {
        if (!tileCoord) {
          return "";
        }
        var z = tileCoord[0];
        var x = tileCoord[1];
        var y = tileCoord[2];

        if (x < 0) {
          x = "M" + (-x);
        }

        y = (-y);

        //zxy
        return "http://localhost:8080/gis/" + x + "/" + y + "/" + z + ".png";
      }
    });
    var baidusource = new ol.source.XYZ({
      projection: 'baidu',
      maxZoom: 18,
      tileUrlFunction: function (tileCoord, ss, e) {
        var x = tileCoord[1];
        var y = tileCoord[2];
        var z = tileCoord[0];
        console.log(x, y, z, ss, e)
        y = -y
        return "http://online3.map.bdimg.com/tile/?qt=vtile&x=" + x + "&y=" + y + "&z=" + z + "&styles=pl&udt=udt=20170908&scaler=1&p=1"
      },
      tileGrid: new ol.tilegrid.TileGrid({
        resolutions: resolutions,
        origin: [0, 0],
        //extent: ol.extent.applyTransform(extent, projzh.ll2bmerc),
        tileSize: [256, 256]
      })
    })

    var baidu_layer = new ol.layer.Tile({
      source: baidu_source
    });

    var map = new ol.Map({
      target: 'map',
      layers: [baidu_layer],
      view: new ol.View({
        center: [12959773, 4853101],
        zoom: 5
      })
    }); 
  </script>

</body>

</html>
